package com.pe1n.yangtaopai.adapter;

import android.content.Context;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.pe1n.yangtaopai.R;
import com.pe1n.yangtaopai.bean.CarBean;
import com.pe1n.yangtaopai.helper.IupDataListener;
import com.pe1n.yangtaopai.helper.IupDataListenerTwo;
import com.pe1n.yangtaopai.page.detail.view.activity.DetailActivity;


import java.util.List;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by My on 2016/11/26.
 */

public class CarBottomReclerAdapter extends RecyclerView.Adapter<CarBottomReclerAdapter.ViewHolder> {
    private Context mContext;//上下文
    private List<CarBean> mList;//数据源
    private AlertDialog alertDialog;
//    private int count=1;//商品的数量
//    private double price=0.0;
    private double priceCount=0.0;//改变加减数量变化的数据;
    //用于接口回调
    private IupDataListener iupDataListener;
    private IupDataListenerTwo iupDataListenerTwo;//对于购物车更新的接口

    public CarBottomReclerAdapter(Context mContext, List<CarBean> mList,IupDataListener iupDataListener,IupDataListenerTwo iupDataListenerTwo) {
        this.mContext = mContext;
        this.mList = mList;
        this.iupDataListener=iupDataListener;
        this.iupDataListenerTwo=iupDataListenerTwo;
    }
    public void updata(List<CarBean> list){
        this.mList=list;
        this.notifyDataSetChanged();
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_car_item_recyclerview_one_item,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
          final CarBean mCarBean=mList.get(position);
          if(mCarBean.isChoice()){
              holder.imageViewSelect.setImageResource(R.drawable.cart_select);
              //后加的
              iupDataListener.updata(0.0,true);
              //后加的
          }
          if(!mCarBean.isChoice()){
              holder.imageViewSelect.setImageResource(R.drawable.cart_select_normalx);
              iupDataListener.updata(0.0,true);
            }
          holder.imageViewSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isClick=mCarBean.isChoice();
                if ( !isClick){
                    holder.imageViewSelect.setImageResource(R.drawable.cart_select);
                    iupDataListener.updata(mCarBean.getMoney()*mCarBean.getAmount(),true);

                }
                else{
                    holder.imageViewSelect.setImageResource(R.drawable.cart_select_normalx);
                    iupDataListener.updata(mCarBean.getMoney()*mCarBean.getAmount(),false);

                }

                mCarBean.setChoice(!isClick);


            }
        });
        if(  mCarBean.getImageViewUrl()!=null){
            Glide.with(mContext).load( mCarBean.getImageViewUrl())
                    .placeholder(R.drawable.ytjx)
                    .into(  holder.mImageViewPic);
        }
        holder.mTextViewPicName.setText(mCarBean.getName());
        holder. mTextViewWeight.setText(mCarBean.getWeight());
        holder.mTextViewMoney.setText(mCarBean.getMoney()+"");
        holder.imgeViewTJ.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCarBean.setAmount(mCarBean.getAmount()+1);
                holder.textView.setText(String.valueOf(mCarBean.getAmount()));
                if(mCarBean.isChoice()){
                iupDataListener.updata(mCarBean.getMoney(),true);}
                //iupDataListener.updata(priceCount+mCarBean.getMoney()*mCarBean.getAmount());
                //iupDataListener.updata(mCarBean.getMoney(),true);
            }

        });

        holder.textView.setText(mCarBean.getAmount()+"");
        holder. imgeViewJS.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCarBean.getAmount() >= 1) {
                    mCarBean.setAmount(mCarBean.getAmount() - 1);
                    holder.textView.setText(String.valueOf(mCarBean.getAmount()));
                     if(mCarBean.isChoice()){
                    iupDataListener.updata(mCarBean.getMoney(),false);}
                    //iupDataListener.updata(mCarBean.getMoney(),false);
                    //double d= priceCount+mCarBean.getMoney()*mCarBean.getAmount();
                    // if(d<0.0){
                    //d=0.0;
                }
                //Double double=priceCount+mCarBean.getMoney()*mCarBean.getAmount();
                //iupDataListener.updata( d);

            }
        });
        holder.mImageViewPic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext, DetailActivity.class);
                intent.putExtra("id",mCarBean.getId());
                intent.putExtra("name",mCarBean.getName());
                mContext.startActivity(intent);

            }
        });
        holder. imageViewDelete.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             AlertDialog.Builder builder=new AlertDialog.Builder(mContext);
             builder.setMessage("您确定要删除吗?")
                     .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                         @Override
                         public void onClick(DialogInterface dialog, int which) {
                             CarBean carBean=new CarBean();
                             carBean.setObjectId(mCarBean.getObjectId());
                             Log.e("deleteName",mCarBean.getName());
                             Log.e("deleteObjectId",mCarBean.getObjectId());
                             carBean.delete(new UpdateListener() {
                                 @Override
                                 public void done(BmobException e) {
                                     if (e==null){
                                         Toast.makeText(mContext,"删除成功啦",Toast.LENGTH_SHORT).show();
                                         iupDataListener.updata(0.0,true);
                                         iupDataListenerTwo.upDataBmob();

                                     }
                                     else{
                                         Toast.makeText(mContext,"失败啦!",Toast.LENGTH_SHORT).show();
                                         Log.e("bmob",e.toString());
                                     }
                                 }
                             });


                         }
                     })
                     .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                         @Override
                         public void onClick(DialogInterface dialog, int which) {
                             alertDialog.dismiss();
                         }
                     });
             alertDialog=builder.create();
             alertDialog.show();

         }
     });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        ImageView imageViewSelect;
        ImageView mImageViewPic;
        private LinearLayout mLinearLayout;
        TextView mTextViewPicName;
        TextView mTextViewWeight;
        TextView mTextViewMoney;//钱
        ImageView imgeViewTJ;//增加
        TextView textView;//数量
        ImageView imgeViewJS;//减少
        ImageView imageViewDelete;//删除

        private int price;
        public ViewHolder(View itemView) {
            super(itemView);
           // ft_car_itemReoItem_textViewName
            imageViewSelect= (ImageView) itemView.findViewById(R.id.ft_car_itemReoItem_imageViewSelect);
            mImageViewPic= (ImageView) itemView.findViewById(R.id.ft_car_itemReoItem_imageViewPic);
            mTextViewPicName= (TextView) itemView.findViewById(R.id.ft_car_itemReoItem_textViewName);
            mTextViewWeight= (TextView) itemView.findViewById(R.id.ft_car_itemReoItem_textViewWeight);
            mTextViewMoney= (TextView) itemView.findViewById(R.id.ft_car_itemReoItem_textViewMoney);
            imgeViewTJ= (ImageView) itemView.findViewById(R.id.ft_car_itemReoItem_imageViewTJ);
            textView= (TextView) itemView.findViewById(R.id.ft_car_itemReoItem_imageViewSL);
            imgeViewJS= (ImageView) itemView.findViewById(R.id.ft_car_itemReoItem_imageViewJS);
            imageViewDelete= (ImageView) itemView.findViewById(R.id.ft_car_itemReoItem_imageViewDelete);
            mLinearLayout= (LinearLayout) itemView.findViewById(R.id.ft_car_itemReoItem_linearLayout);
        }
    }
}
